package com.example.chenhuamou.freenews.view.discover;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.chenhuamou.freenews.R;
import com.example.chenhuamou.freenews.view.common.entity.JcodeEntity;
import com.example.chenhuamou.freenews.view.discover.adapter.DiscoverAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenhuamou on 2017/9/14.
 */

public class DiscoverFragment extends android.support.v4.app.Fragment implements SwipeRefreshLayout.OnRefreshListener {

    private static final int TYPE_FOOTER = 0;
    private static final int ITEM_HAS_IMG = 1;
    private static final int ITEM_NO_IMG = 2;


    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout mRefreshLayout;

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    // 适配器
    private DiscoverAdapter mAdapter;
    // 数据源
    private List<JcodeEntity> data = new ArrayList<>();
    // 当前刷新页码
    private int pageIndex = 1;

    private static final String HOST = "http://www.jcodecraeer.com";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_discover, container, false);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    /*
    * 点击浮动按钮
    * */
    @OnClick(R.id.floatBtn)
    public void OnClick(View view) {
        mRecyclerView.smoothScrollToPosition(0);
    }

    /*
    * 初始化 View
    * */
    private void initView() {
        // 设置颜色
        mRefreshLayout.setColorSchemeColors(new int[]{getResources().getColor(R.color.colorPrimary), android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light});
        // 设置监听
        mRefreshLayout.setOnRefreshListener(this);

        // 设置 LayoutManager
        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(llm);
        // 高度是固定的，设置这个可以提高效率
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());

        //TODO: 自定义分割线
        // 自定义分割线
//        mRecyclerView.addItemDecoration(new Defa);

        // 设置 adapter
        mAdapter = new DiscoverAdapter(getActivity(), data);
        // 设置尾部刷新视图
        mAdapter.addFooterView(R.layout.view_footer_refresh);
        mAdapter.setmOnItemClickListener(new DiscoverAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                // 跳转到资讯详情页面
            }
        });
        mRecyclerView.setAdapter(mAdapter);

        // TODO: 监听滚动
        // 滚动监听，滚到当前最后一项的时候，加载更多
//        mRecyclerView.addOnScrollListener(new End);
    }

    /**
     * Called when a swipe gesture triggers a refresh.
     */
    @Override
    public void onRefresh() {

    }



}
